/**

=blacksmith/install - Installer

The install module installs Blacksmith on to a Blacksmith File System.

*/



INSTALL = {}


INSTALL.out = null

INSTALL.multiparts = null

INSTALL.progress = null

INSTALL.files = [
    '/install/blacksmith/__dir__.js',
    '/install/blacksmith/cannon.js',
    '/install/blacksmith/dat.js',
    '/install/blacksmith/doc.js',
    '/install/blacksmith/game.js',
    '/install/blacksmith/ji.js',
    '/install/blacksmith/math.js',
    '/install/blacksmith/boot.html',
    '/install/blacksmith/term.js',
    '/install/blacksmith/test.js',
    '/install/blacksmith/three.js'
]



INSTALL.read = function(path, fn) {
    /**
     * Reads a static file from current host.
     */
    
    var xhr = new XMLHttpRequest()
    xhr.open('GET', path)
    xhr.onreadystatechange = function () {
        if (typeof fn !== 'undefined' && this.status === 200 && this.readyState === 4)
            fn(this.responseText)
    }
    xhr.send()

}



INSTALL.finalise = function() {
    /**
     * Ends the installation with a nice message.
     */
    
    INSTALL.out.print('installation complete')
    INSTALL.out.print(BLACKSMITH.element('a', {href: '/blacksmith/term'}, ['start']))

}



INSTALL.installFile = function(path) {

    INSTALL.progress = BLACKSMITH.element(INSTALL.out.print(path.replace('/install', '') + ' '), 'install_progress', ['0%'])
    INSTALL.progress.style.color = '#e06c1f'
    INSTALL.read(path, function (m) {
        var dst = BLACKSMITH.File(path.replace('/install', ''))
        dst.write(m)
        dst.close(function (m) {
            if (m.hasOwnProperty('error'))
                return INSTALL.out.print(m)
            else if (m.hasOwnProperty('multiparts'))
                INSTALL.multiparts = m.multiparts
            else if (m.hasOwnProperty('multipart')) {
                INSTALL.progress.innerHTML = ~~((m.multipart / INSTALL.multiparts) * 100) + '%'
            } else if (m.hasOwnProperty('success')) {
                INSTALL.progress.innerHTML = '100%'
                INSTALL.progress.style.color = '#00ff00'
                if (INSTALL.files.length > 0)
                     INSTALL.installFile(INSTALL.files.shift())
                else
                     INSTALL.finalise()
            }
        })
    })

}



INSTALL.install = function() {

    BLACKSMITH.OS.mkdir('/blacksmith', function () {
        INSTALL.installFile(INSTALL.files.shift())
    })

}



BLACKSMITH.element(document.head, 'syle', {'type': 'text/css'}, [
    'html {',
    '  height: 100%;',
    '}',
    'body {',
    '  height: 100%;',
    '}',
    '.install_progress {',
    '  min-width: 2px',
    '  color: green',
    '}'
])

print(BLACKSMITH.environ.baseUrl)

requirejs.config({baseUrl:BLACKSMITH.environ.baseUrl+'/install/',waitSeconds:15})

require(['blacksmith/term'], function() {


	
BLACKSMITH.OS.identify(document.location, function (user) {
    
    INSTALL.out = TERM.Term(document.body)
    INSTALL.out.print(BLACKSMITH.environ.version + ' installation')
    BLACKSMITH.OS.listdir('/', function (files) {
    
        if (files.hasOwnProperty('error') && files.error.indexOf('/: No such') === 0) {
            INSTALL.out.print('no previous installation found')
            BLACKSMITH.OS.system('/', ['Z/'], function (resp) {
                if (resp.hasOwnProperty('error'))
                    return INSTALL.out.print(resp)
                INSTALL.out.print('format successful')
                INSTALL.install()
            })

        } else {

            INSTALL.out.print('previous installation found, skipping format')
            INSTALL.install()

        }

    })

})




})

